package com.example.demo.story;

import cn.hutool.system.UserInfo;
import com.google.common.base.Objects;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 傻男人 <244191347@qq.com>
 * @version 1.0
 * @description todo
 * @date 2024-02-02
 **/
@Slf4j
public class XX {

    public static void main(String[] args) throws IOException {
        YYY yyy = new YYY();
        yyy.setWidth(800);
        yyy.setHeight(600);
        yyy.setBackgroundFile(new File("C:\\Users\\13278\\Pictures\\门店3.jpg"));
        yyy.setOutFile(new File("C:\\Users\\13278\\Pictures\\海报测试.jpg"));


        XXX xxx = new XXX();
        xxx.setFile(new File("C:\\Users\\13278\\Pictures\\门店2.jpg"));
        xxx.setStartX(200);
        xxx.setStartY(200);
        xxx.setEndX(300);
        xxx.setEndY(300);
        xxx.setType(1);

       XXX xxx1 = new XXX();
        xxx1.setDescribe("this is test 测试");
        xxx1.setStartX(400);
        xxx1.setStartY(450);
        xxx1.setEndX(600);
        xxx1.setEndY(550);
        xxx1.setType(0);
        xxx1.setColor(Color.RED);
        yyy.setContent(new ArrayList<XXX>(){{add(xxx);add(xxx1);}});
        test(yyy);
    }

    public static void test(YYY yyy) throws IOException {
        final int width = yyy.getWidth();
        final int height = yyy.getHeight();

        // 创建一个新的 BufferedImage 对象作为画布
        BufferedImage posterImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        // 获取 Graphics2D 对象以便绘图
        Graphics2D graphics = posterImage.createGraphics();

        // 设置抗锯齿和渲染质量
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        // 加载并绘制背景图片
        File backgroundFile = yyy.getBackgroundFile();
        BufferedImage backgroundImage = ImageIO.read(backgroundFile);
        graphics.drawImage(backgroundImage, 0, 0, width, height, null);

        final List<XXX> content = yyy.getContent();
        content.forEach(item->{
            final int type = item.getType();
            final int startX = item.getStartX();
            final int startY = item.getStartY();
            final int endX = item.getEndX();
            final int endY = item.getEndY();
            if(Objects.equal(1,type)){
                final File file = item.getFile();
                //将图片等比压缩
                final int imageWidth = item.width();
                final int imageHeight = item.height();
                BufferedImage image = null;
                try {
                    image = ImageIO.read(file);
                    if(!Objects.equal(image.getWidth(),imageWidth) || Objects.equal(image.getHeight(),imageHeight)){
                        image = resizeImage(image,item.width(),item.height());
                    }
                    graphics.drawImage(image, startX, startY, endX, endY, null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(Objects.equal(0,type)){
                final Font font = item.getFont();
                final Color color = item.getColor();
                final String describe = item.getDescribe();
                graphics.setFont(font);
                graphics.setColor(color);
                graphics.drawString(describe,startX,startY);
            }
        });

        // 清理资源并保存海报
        graphics.dispose();

        // 保存生成的海报到文件
        ImageIO.write(posterImage, "PNG", yyy.getOutFile());

        System.out.println("海报已成功生成！");
    }

    public static BufferedImage resizeImage(BufferedImage inputImage, int maxWidth, int maxHeight) throws IOException {
        // 获取原始图片的宽高
        int imageWidth = inputImage.getWidth();
        int imageHeight = inputImage.getHeight();

        // 计算缩放比例
        double scaleWidth = (double) maxWidth / imageWidth;
        double scaleHeight = (double) maxHeight / imageHeight;
        double scaleFactor = Math.min(scaleWidth, scaleHeight); // 等比缩放，取较小的比例

        // 创建新的缩放后的图像尺寸
        int newWidth = (int) (imageWidth * scaleFactor);
        int newHeight = (int) (imageHeight * scaleFactor);

        // 创建一个新的BufferedImage对象用于存储缩放后的图片
        BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, inputImage.getType());

        // 创建Graphics2D对象以在新图像上绘图
        Graphics2D g = resizedImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // 设置双线性插值提高质量

        // 进行缩放操作
        AffineTransform at = AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
        g.drawRenderedImage(inputImage, at);

        // 清理资源
        g.dispose();
        return resizedImage;
    }

}
